package com.mx.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.mx.dao.PersonDao;
import com.mx.entity.Person;
@Repository
public class PersonDAOimpl implements PersonDao {
  //通过我们的session得到数据
	@Autowired
	 private  SessionFactory  sessionfactory;
	
	@Override
	public List<Person> findAllPerson() {
          Session session=sessionfactory.openSession();
          Query  query=session.createQuery("from Person");
		
          List<Person> list=query.list();
		  session.close();
          return list;
	}

	@Override
	public void add(Person person) {
		// TODO Auto-generated method stub
		Session session=sessionfactory.openSession();
		Transaction  transaction=session.beginTransaction();
		session.save(person);
		transaction.commit();
	}

	@Override
	public void delPerson(String id) {
		Session session=sessionfactory.openSession();
		Transaction  transaction=session.beginTransaction();
		int ida=new Integer(id);
		Person  person=(Person) session.get(Person.class, ida);
		session.delete(person);
		transaction.commit();
		session.close();
	}

	@Override
	public Session getSession() {
		// TODO Auto-generated method stub
		return null;
	}

}
